const {
  async
} = require("utils/runtime")
var zhenzisms = require('./utils/zhenzisms.js');
// pages/addAddress/addAddress.js

App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  //支付钱，名字，随意更改， 行id（商品行id）
  pay_product(totalCost, merchandiseDescription, merchandiseSnapshot, merchandiseRecordID) {
    return new Promise((resolve, reject) => {
      // 支付示例代码
      console.log(totalCost, merchandiseDescription, merchandiseSnapshot, merchandiseRecordID);
      let params = {
        totalCost: 0.01,
        merchandiseDescription: merchandiseDescription,
        merchandiseSnapshot: merchandiseSnapshot,
        merchandiseSchemaID: 113624,
        merchandiseRecordID: merchandiseRecordID
      }

      wx.BaaS.pay(params).then(res => {
        // success. 支付请求成功响应，可以在 res 中拿到 transaction_no 和支付结果信息
        // console.log(res)
        wx.setStorageSync('res', res)
        resolve(res)


      }, err => {
        wx.showToast({
          title: '支付失败',
          icon:'none'
        })
        // 未完成用户授权或发生网络异常等
        console.log(err)
        // HError 对象
        if (err.code === 603) {
          console.log('用户尚未授权')
        } else if (err.code === 607) {
          
          console.log('用户取消支付')
        } else if (err.code === 608) {
          console.log(err.message)
        }
      })
    })
  },
  pay_service_items(totalCost, merchandiseDescription, merchandiseSnapshot, merchandiseRecordID) {
    // 支付示例代码
    return new Promise((resolve, reject) => {
      let params = {
        totalCost: totalCost,
        merchandiseDescription: merchandiseDescription,
        merchandiseSnapshot: merchandiseSnapshot,
        merchandiseSchemaID: 112305,
        merchandiseRecordID: merchandiseRecordID
      }

      wx.BaaS.pay(params).then(res => {
        // success. 支付请求成功响应，可以在 res 中拿到 transaction_no 和支付结果信息
        console.log(res)
      }, err => {
        // 未完成用户授权或发生网络异常等
        console.log(err)
        wx.showToast({
          title: '支付失败',
          icon:'none'
        })
        // HError 对象
        if (err.code === 603) {
          console.log('用户尚未授权')
        } else if (err.code === 607) {
          wx.showToast({
            title: '取消支付',
            icon:'none'
          })
          console.log('用户取消支付')
        } else if (err.code === 608) {
          console.log(err.message)
        }
      })
    })
  },
  navigateToDeTail(id) {
    //id是产品id
    setTimeout(function (e) {
      wx.navigateTo({
        url: '/pages/post-detail/post-detail?id=' + id,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }, 500)
  },
  navigateToService(id) {
    //id是服务（技师）id
    setTimeout(function (e) {
      wx.navigateTo({
        url: '/pages/post-detail/post-detail-service/post-detail-service?id=' + id,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }, 500)
  },
  navigateToBuy(id) {
    //id是订单id
    setTimeout(function (e) {
      wx.redirectTo({
        url: '/pages/post-detail/post-detail-buy/post-detail-buy?id=' + id,
        success: (res) => {},
        fail: (res) => {},
        complete: (res) => {},
      })
    }, 500)
  },
  globalData: {
    userInfo: null,
    id: '', //用户知晓云id
    phone: '',
    item: [], //用户信息
    product: [], // 产品
    product_sum: 0,
    all: [],
    service: [],
    service_sum: 0,
    service_items: [],
    service_items_sum: 0,
    latitude: 0,
    longitude: 0,
    BarHeight: 0,
    isOnload: false,
    dingdanhao: '',
    isPay: false,
    res: {},
    address: '',
    service_name:'',
    service_items_pic:'',
    member_id:'',
    xianshi:[],
    isLike:[],
    zhuzhiaddress:'',
    key:[],
    youhui:[]
  },
  getMessage(phone , id ,ppp){
    
    zhenzisms.client.init("https://sms_developer.zhenzikj.com", 109147, "4d302f62-76ff-4653-8742-4c1f99b3649d"
    );
    var params = {};
    params.number = String(phone);
    // params.number = String(17635862457);
    console.log(phone);
    // console.log(that.data.item[i]);
    params.templateId = id;
    // var code = zhenzisms.client.createCode(4, 60, params.number); //生成验证码
    var templateParams = ppp;
    params.templateParams = templateParams;
    // console.log(code);
    zhenzisms.client.send(function (res) {
      console.log(res)
    }, params);
  },
  goyouhui:function(id){
    wx.navigateTo({
      url: '/pages/youhui/youhui?id='+id,
    })
  },
  pay_youhui(totalCost, merchandiseDescription, merchandiseSnapshot, merchandiseRecordID){
    return new Promise((resolve, reject) => {
      let params = {
        totalCost: 0.01,
        merchandiseDescription: merchandiseDescription,
        merchandiseSnapshot: merchandiseSnapshot,
        merchandiseSchemaID: 115836,
        merchandiseRecordID: merchandiseRecordID
      }

      wx.BaaS.pay(params).then(res => {
        // success. 支付请求成功响应，可以在 res 中拿到 transaction_no 和支付结果信息
        console.log(res)
      }, err => {
        // 未完成用户授权或发生网络异常等
        console.log(err)
        wx.showToast({
          title: '支付失败',
          icon:'none'
        })
        // HError 对象
        if (err.code === 603) {
          console.log('用户尚未授权')
        } else if (err.code === 607) {
          wx.showToast({
            title: '取消支付',
            icon:'none'
          })
          console.log('用户取消支付')
        } else if (err.code === 608) {
          console.log(err.message)
        }
      })
    })
  },
  cost(dengji,cost ){
    if(dengji == 0){
      return parseFloat(parseInt(cost)**0.98).toFixed(2)
    }else if(dengji == 1){
      return parseFloat(parseInt(cost)**0.95).toFixed(2)
    }else if(dengji == 2){
      return parseFloat(parseInt(cost)*0.9).toFixed(2)
    }else if(dengji == 3){
      return parseFloat(parseInt(cost)*0.8).toFixed(2)
    }
    
  },
  onLaunch: function () {
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.BaaS = requirePlugin('sdkPlugin')
    //让插件帮助完成登录、支付等功能
    wx.BaaS.wxExtend(wx.login, wx.getUserInfo, wx.requestPayment)

    let clientID = 'fb967cbffa3a1cba12e1' // 应用名称: 奈优蜜的第一个小程序
    wx.BaaS.init(clientID)
    // 微信用户登录小程序
    // 微信用户登录小程序
    wx.BaaS.auth.loginWithWechat().then(user => {
      // 登录成功
      console.log(user)
    }, err => {
      // 登录失败
      console.log("登录失败")
    })
  },


})